package ph.com.gs3.formalistics.view.document.fields;

import java.util.List;

import ph.com.gs3.formalistics.global.constants.Constants;
import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.vo.business.form.FormFieldData;
import ph.com.gs3.formalistics.view.document.abstractfields.GroupTypeFieldView;
import android.content.Context;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class RadioButtonGroupField extends GroupTypeFieldView {

	public static final String TAG = RadioButtonGroupField.class.getSimpleName();

	private RadioGroup radioGroup;

	public RadioButtonGroupField(Context context, FormFieldData formField) {
		super(context, formField);
		createView();
	}

	private void createView() {

		List<String> options = formField.getOptions();
		String selectedValue = null;
		String selectedValueRaw = formField.getValue();

		if (selectedValueRaw != null) {
			if (selectedValueRaw.contains("|")) {
				selectedValue = selectedValueRaw.split("\\|")[0];
			} else {
				selectedValue = selectedValueRaw;
			}
		}

		// Initialize the view
		radioGroup = new RadioGroup(getContext());

		radioGroup.setOrientation(RadioGroup.VERTICAL);
		radioGroup.setTag(formField.getId());
		radioGroup.setPadding(0, 0, GROUP_VIEWS_RIGHT_PADDING, 0);

		boolean hasCheckedButton = false;
		int checkedButtonId = -1;

		for (int i = 0; i < options.size(); i++) {

			String optionString = options.get(i).trim();
			String optionLabel = optionString;
			String optionStringValue = optionString;

			RadioButton radioButton = new RadioButton(getContext());
			radioButton.setLayoutParams(lpFillWidth);

			radioButton.setId(i);
			radioButton.setTypeface(Constants.getApplicationInstance().getGlobalTypeface());

			// Check if the option is value label pair, if it is, divide the option string
			// with | and assign the label and value properly
			if (optionString.contains("|")) {
				String[] splittedOptionString = optionString.split("\\|");

				optionStringValue = splittedOptionString[0];
				optionLabel = splittedOptionString[1];
			}

			radioButton.setText(optionLabel);

			if (selectedValue != null) {
				if (selectedValue.equals(optionStringValue)) {
					hasCheckedButton = true;
					checkedButtonId = radioButton.getId();
				}
			}

			radioButton.setEnabled(isEnabled);
			radioGroup.addView(radioButton);

		}

		this.addView(radioGroup);

		if (hasCheckedButton) {
			radioGroup.check(checkedButtonId);
		}

	}

	@Override
	public String getValue() {

		int collectionChildCount = radioGroup.getChildCount();

		String value = "";

		// Search which value is selected and return it
		for (int index = 0; index < collectionChildCount; index++) {
			if (radioGroup.getChildAt(index) instanceof RadioButton) {
				RadioButton rbField = (RadioButton) radioGroup.getChildAt(index);
				if (rbField.isChecked()) {
					value = rbField.getText().toString();
				}
			}
		}

		return value;
	}

	@Override
	public void setValue(String value) {
		int collectionChildCount = radioGroup.getChildCount();
		for (int index = 0; index < collectionChildCount; index++) {
			if (radioGroup.getChildAt(index) instanceof RadioButton) {
				RadioButton rbField = (RadioButton) radioGroup.getChildAt(index);
				if (rbField.getText().toString().equals(value)) {
					rbField.setChecked(true);
				}
			}
		}
	}

	@Override
	protected void initializeChangeListener() {
		FLLogger.w(TAG, "initializeChangeListener not yet implemented");
	}

	@Override
	public void showError(String errorMessage) {

		int radioButtonCount = radioGroup.getChildCount();

		if (radioButtonCount > 0) {
			// set error on the first radio button found
			final RadioButton rbFirstField = (RadioButton) radioGroup.getChildAt(0);
			rbFirstField.setError(PROMPT_FIELD_REQUIRED);

			radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

				@Override
				public void onCheckedChanged(RadioGroup group, int checkedId) {
					rbFirstField.setError(null);
				}
			});

		}
	}

}
